package server.controller.affiche;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import server.db.primary.mapper.map.AfficheAppMapper;
import server.db.primary.model.map.AfficheApp;
import server.tool.Res;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;

@RestController
public class AfficheAppController {

    @Autowired
    AfficheAppMapper afficheAppMapper;

    /**
     * 插入数据
     */
    @RequestMapping("/AfficheApp/Insert")
    public Res  afficheByInsert(AfficheApp afficheApp) {
        int insert = afficheAppMapper.insert(afficheApp);
        return Res.success(insert);
    }


    /**
     * 根据所有流水号查询所有的数据
     * @param number
     * @return
     */
    @RequestMapping("/AfficheApp/affcheAppBynumberId")
    public Res affcheAppBynumberId(Long number) {

        List<AfficheApp> afficheApps = afficheAppMapper.selectByNumberId(number);
        return Res.success(afficheApps);
    }

    @RequestMapping("/AfficheApp/DelectId")
    public Res affcheAppDelectId(long id) {
        int i = afficheAppMapper.deleteByPrimaryKey(id);
        return Res.success(i);
    }



    /**
     * 实现文件上传.....
     * */
    @RequestMapping("/AfficheApp/upload")
    public Res fileupload(AfficheApp afficheApp,MultipartFile file, HttpServletRequest request) {


        Map<String, Object> result = new HashMap<>();

        String originalFilename = file.getOriginalFilename();
        String substring1 = originalFilename.substring(originalFilename.lastIndexOf("."));
        afficheApp.setAppName(originalFilename);

        //获取文件的后缀名
        String substring2 = originalFilename.substring(originalFilename.indexOf("."));

        if (!originalFilename.endsWith(substring2)) {
            return Res.failure(null,"文件格式错误");
        }
        String realPath = request.getServletContext().getRealPath("/");
        String substring = realPath.substring(0, realPath.lastIndexOf("\\"));

        File folder = new File(realPath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        String newName = UUID.randomUUID() + substring2;

        try {
            file.transferTo(new File(folder, newName));
            String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + newName;
            //存localhost
            afficheApp.setData(url);
            //路径
//            afficheApp.setData(realPath + newName);

            result.put("status", "success");
            result.put("message", "上传成功");
            result.put("url", url);
        } catch (IOException e) {
            result.put("status", "error");
            result.put("messgae", e.getMessage());
        }
        int insert = afficheAppMapper.insert(afficheApp);
        System.out.println("影响行数---->"+insert);
        return Res.success(result);
    }
}
